package com.dyzx.hbb.modules.fcxs.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.common.controller.BaseValidController;
import com.dyzx.hbb.common.utils.DateFormat;
import com.dyzx.hbb.modules.fcxs.entity.FcxsDiscount;
import com.dyzx.hbb.modules.fcxs.entity.FcxsOrder;
import com.dyzx.hbb.modules.fcxs.query.FcxsDiscountQuery;
import com.dyzx.hbb.modules.fcxs.service.FcxsDiscountService;
import com.dyzx.hbb.modules.fcxs.service.FcxsOrderService;
import com.dyzx.hbb.modules.fcxs.vo.FcxsDiscountVO;
import com.dyzx.hbb.modules.fcxs.vo.FcxsDiscountUVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 优惠管理控制器
 * 
 * 功能说明：负责处理房地产销售系统中优惠信息的创建、查询、更新和删除等操作
 * 主要职责：
 * - 提供优惠信息的CRUD接口
 * - 支持按条件查询优惠列表
 * - 处理优惠信息添加和编辑时的订单关联验证
 * - 支持按订单ID、类型、状态等条件筛选优惠数据
 * 
 * @author 开发团队
 * @version 1.0.0
 * @since 2023-05-30
 */
@Tag(name = "06.优惠管理", description = "优惠管理相关接口，包括优惠的创建、查询、更新和删除等操作")
@RestController
@RequestMapping("/fcxs/discount")
public class FcxsDiscountController extends BaseValidController<
    FcxsDiscountService,
    FcxsDiscount,
    FcxsDiscountQuery,
    FcxsDiscountVO,
    FcxsDiscountUVO
> {
    @Autowired
    private FcxsDiscountService fcxsDiscountService;

    @Autowired
    private FcxsOrderService fcxsOrderService;

    /**
     * 获取基础服务类
     * @return FcxsDiscountService实例
     */
    @Override
    protected FcxsDiscountService getBaseService() {
        return fcxsDiscountService;
    }

    /**
     * 构建查询条件包装器
     * @param query 查询参数
     * @return 查询条件包装器
     */
    @Override
    protected MyLambdaQueryWrapper<FcxsDiscount> queryWrapper(FcxsDiscountQuery query) {
        MyLambdaQueryWrapper<FcxsDiscount> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(FcxsDiscount::getId)
                .select(FcxsDiscount::getOrderId)
                .fcxsOrderNo(FcxsDiscount::getOrderId).as("order_no")
                .select(FcxsDiscount::getType)
                .select(FcxsDiscount::getAmt)
                .select(FcxsDiscount::getRemark)
                .select(FcxsDiscount::getStatus)
                .select(FcxsDiscount::getCreateDate)
                .select(FcxsDiscount::getUpdateDate)
                .organName(FcxsDiscount::getOrganId).as("organ_name")
                .userName(FcxsDiscount::getOperatorId).as("operator_name");
        if (query != null){
            queryWrapper
                    .eq(query.getType()!=null, FcxsDiscount::getType, query.getType())
                    .eq(query.getStatus()!=null, FcxsDiscount::getType, query.getStatus())
                    .dateBetween(query.getCreate_date()!=null, FcxsDiscount::getCreateDate, DateFormat.DATE, query.getCreate_date())
                    .byFcxsOrderNo(query.getOrder_no()!=null, FcxsDiscount::getOrderId, query.getOrder_no())
                    .eq(query.getOrder_id()!=null, FcxsDiscount::getOrderId, query.getOrder_id());
        }
        return queryWrapper;
    }

    /**
     * 构建详情查询条件包装器
     * @return 详情查询条件包装器
     */
    @Override
    protected MyLambdaQueryWrapper<FcxsDiscount> queryInfoWrapper() {
        MyLambdaQueryWrapper<FcxsDiscount> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(FcxsDiscount::getId)
                .select(FcxsDiscount::getOrderId)
                .select(FcxsDiscount::getType)
                .select(FcxsDiscount::getAmt)
                .select(FcxsDiscount::getRemark)

                .select(FcxsDiscount::getStatus)
                .select(FcxsDiscount::getCreateDate)
                .select(FcxsDiscount::getUpdateDate)
                .organName(FcxsDiscount::getOrganId).as("organ_name")
                .userName(FcxsDiscount::getOperatorId).as("operator_name");
        return queryWrapper;
    }

    /**
     * 添加优惠信息时的验证
     * @param entity 优惠信息VO
     * @return 验证通过的优惠实体
     */
    @Override
    protected FcxsDiscount addValid(FcxsDiscountVO entity) {
        FcxsOrder fcxsOrder = fcxsOrderService.fondByOrderIdOrOrderNo(entity.getOrderId(), entity.getOrderNo());
        if(fcxsOrder == null){
            throw new RuntimeException("找不到订单");
        }
        FcxsDiscount data = new FcxsDiscount();
        BeanUtil.copyProperties(entity, data);
        data.setOrderId(fcxsOrder.getId());
        return data;


    }

    /**
     * 编辑优惠信息时的验证
     * @param entity 优惠信息更新VO
     * @return 验证通过的优惠实体
     */
    @Override
    protected FcxsDiscount editValid(FcxsDiscountUVO entity) {
        if (entity.getOrderId() != null) {
            FcxsDiscount data = new FcxsDiscount();
            FcxsOrder fcxsOrder = fcxsOrderService.fondByOrderId(entity.getOrderId());
            if (fcxsOrder == null) {
                throw new RuntimeException("找不到订单");
            }

            BeanUtil.copyProperties(entity, data);
            data.setOrderId(fcxsOrder.getId());

            return data;
        } else {
            return null;
        }
    }
}
